﻿using System;
using UnityEngine;

[Serializable]
public class SerializableTexture2D
{
    public byte[] bytes;
    [NonSerialized]
    private Texture2D m_Tex;

    public SerializableTexture2D(Texture2D tex)
    {
        this.tex = tex;
        this.bytes = tex.EncodeToPNG();
    }

    public static implicit operator Texture2D(SerializableTexture2D v)
    {
        return v.tex;
    }

    public static implicit operator SerializableTexture2D(Texture2D v)
    {
        return new SerializableTexture2D(v);
    }

    public Texture2D tex
    {
        get
        {
            if ((this.m_Tex == null) && (this.bytes != null))
            {
                this.m_Tex = new Texture2D(0, 0, TextureFormat.ARGB32, false, false);
                this.m_Tex.filterMode = FilterMode.Point;
                this.m_Tex.LoadImage(this.bytes);
                this.m_Tex.Apply(false, true);
            }
            return this.m_Tex;
        }
        set
        {
            this.m_Tex = value;
        }
    }
}

